package edu.xidian.bdilab.jrbigdata.controller.government;

import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.service.government.register.GovernmentRegisterService;
import edu.xidian.bdilab.jrbigdata.util.DecryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName GoveRegisterController
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/3/29 下午7:45
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@CrossOrigin
@RestController
@Api(tags = {"governmentRegister-政府用户注册功能"})
public class GoveRegisterController {
    @Autowired
    GovernmentRegisterService governmentRegisterService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "govrenmentCode", value = "政府编码"),
            @ApiImplicitParam(name = "fkUserName", value = "用户名"),
            @ApiImplicitParam(name = "departmentName", value = "政府部门名称"),
            @ApiImplicitParam(name = "principalName", value = "负责人姓名"),
            @ApiImplicitParam(name = "principalPhone", value = "负责人手机号"),
            @ApiImplicitParam(name = "password", value = "密码")
    })
    @RequestMapping(value = "government/register", method = RequestMethod.POST)
    public ResponseResult register(@RequestParam int govrenmentCode,
                                   @RequestParam String fkUserName,
                                   @RequestParam String departmentName,
                                   @RequestParam String principalName,
                                   @RequestParam String principalPhone,
                                   @RequestParam String password){

        ResponseResult responseResult = new ResponseResult();

        if (governmentRegisterService.checkGovernmentCode(govrenmentCode) == false){
            responseResult.setSuccess(false);
            responseResult.setCode(GovernmentCode.REGISTER_FAIL_GOVERNMENTCODE.getCode());
            responseResult.setCode(GovernmentCode.REGISTER_FAIL_GOVERNMENTCODE.getMessage());
            return responseResult;
        }else if(governmentRegisterService.checkUserName(fkUserName) == false){
            responseResult.setSuccess(false);
            responseResult.setCode(GovernmentCode.REGISTER_FAIL_USERNAME.getCode());
            responseResult.setMessage(GovernmentCode.REGISTER_FAIL_USERNAME.getMessage());
            return responseResult;
        }else {
            governmentRegisterService.setGovernmentInfo(govrenmentCode, fkUserName, departmentName, principalName, principalPhone);
            String md5Password = DecryptUtils.getMd5(password);
            governmentRegisterService.setGovernmentUser(fkUserName, md5Password);

            responseResult.setSuccess(true);
            responseResult.setCode(GovernmentCode.REGISTER_SUCCEESS.getCode());
            responseResult.setMessage(GovernmentCode.REGISTER_SUCCEESS.getMessage());
        }

        return responseResult;

    }
}
